import db as src_db
import target_db as target_db
import concurrent.futures
import sys
import time

def sync(i):
    max_id_sql = "select max(id) as id from " + get_table_name(i)
    with concurrent.futures.ProcessPoolExecutor(max_workers=1) as executor:
        with target_db.UsingMysql() as um:
            um.cursor.execute(max_id_sql);
            id = um.cursor.fetchone()["id"]
        with src_db.UsingMysql() as um:
            page_sql = "select * from " + get_table_name(i) + " where id>" + str(id) + " limit 0, 5000 ";
            um.cursor.execute(page_sql)
            rs = um.cursor.fetchall()
            while (len(rs) > 0):
                list = []
                for data in rs:
                    list.append({"data": data, "index": i})
                executor.submit(save, list)
                page_sql = "select * from " + get_table_name(i) + " where id>" + str(id) + " limit 0, 5000 ";
                um.cursor.execute(page_sql)
                rs = um.cursor.fetchall()
                id = rs[len(rs) - 1]["id"]
            
def get_table_name(i):
    index = "001"
    if(int(i) < 10):
        index = '00'+i
    else:
        index = '0'+i
    return "min5data" + index;

def save(list):
    start_time = time.time()
    with target_db.UsingMysql() as um:
        for rs in list:
            data = rs["data"]
            index = rs["index"]
            sql = "insert into " + get_table_name(index) + " (`minbasicdataID`,`date`,`datetime`,stockID,openPrice,highPrice,lowPrice,closePrice,volume,amount,id)"
            sql += " values('" + data["minbasicdataID"] + "','" + data["date"] + "','" + str(data["datetime"]) + "','" + data["stockID"] + "'," + str(data["openPrice"]) + "," + str(data["highPrice"]) + "," + str(data["lowPrice"]) + "," + str(data["closePrice"])
            sql += "," + str(data["volume"]) + "," + str(data["amount"]) + "," + str(data["id"])
            sql += ");"
            um.cursor.execute(sql)
            # print("[")
        
        um.commit()
    end_time = time.time()
    print('处理数据量：' + str(len(list)) + '  总共的时间为:', round(end_time - start_time, 3),'secs')

if __name__ == '__main__':
    args = ""
    if len(sys.argv) > 1:
        args = sys.argv[1]
    sync(args)